package cn.com.darly.videoandroidpm.ui.conversation.video;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;

import com.darly.common.ToastApp;
import com.darly.dview.common.SCfg;
import com.github.faucamp.simplertmp.RtmpHandler;
import com.seu.magicfilter.utils.MagicFilterType;

import net.ossrs.yasea.SrsCameraView;
import net.ossrs.yasea.SrsEncodeHandler;
import net.ossrs.yasea.SrsPublisher;
import net.ossrs.yasea.SrsRecordHandler;

import java.io.IOException;
import java.net.SocketException;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseFragment;

/**
 * 视频录制Fragment
 * @author Darly/张宇辉/2018/5/30 16:18
 * @version 1.0/cn.com.darly.videoandroidpm.ui.conversation.video
 */
public class VideoPublishFragment extends BaseFragment implements SrsEncodeHandler.SrsEncodeListener, RtmpHandler.RtmpListener, SrsRecordHandler.SrsRecordListener {

    private String ownRTMP;//这个是自己的流媒体地址

    private SrsCameraView glsurfaceview_camera;

    private SrsPublisher mPublisher;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ownRTMP = ((VideoConnectionActivity) activity).getOwnRTMP();
    }

    @Override
    protected int root() {
        return R.layout.fragment_video_publish;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        glsurfaceview_camera = rootView.findViewById(R.id.id_connect_glsurfaceview_camera);
        mPublisher = new SrsPublisher(glsurfaceview_camera);
    }

    @Override
    protected void loadData() {
//编码状态回调
        mPublisher.setEncodeHandler(new SrsEncodeHandler(this));
        mPublisher.setRecordHandler(new SrsRecordHandler(this));
        //rtmp推流状态回调
        mPublisher.setRtmpHandler(new RtmpHandler(this));
        //预览分辨率
        mPublisher.setPreviewResolution(SCfg.getWidth()/4, SCfg.getHeight()/4);
        //推流分辨率
        mPublisher.setOutputResolution(SCfg.getWidth()/4, SCfg.getHeight()/4);
        mPublisher.switchToSoftEncoder();
        //传输率
        mPublisher.setVideoHDMode();
        //开启美颜（其他滤镜效果在MagicFilterType中查看）
        mPublisher.switchCameraFilter(MagicFilterType.BEAUTY);
        //打开摄像头，开始预览（未推流）
        mPublisher.startCamera();
        mPublisher.switchCameraFace(1);

        mPublisher.startPublish(ownRTMP);
        mPublisher.startCamera();
    }

    @Override
    protected void initListener() {

    }


    @Override
    public void onResume() {
        super.onResume();
        mPublisher.resumeRecord();
    }

    @Override
    public void onPause() {
        super.onPause();
        mPublisher.pauseRecord();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPublisher.stopPublish();
        mPublisher.stopRecord();
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mPublisher.stopEncode();
        mPublisher.stopRecord();
        mPublisher.setScreenOrientation(newConfig.orientation);
        mPublisher.startEncode();
        mPublisher.startCamera();
    }

    @Override
    public void onNetworkWeak() {
    }

    @Override
    public void onNetworkResume() {

    }

    @Override
    public void onEncodeIllegalArgumentException(IllegalArgumentException e) {
        handleException(e);
    }

    private void handleException(Exception e) {
        try {
            mPublisher.stopPublish();
            mPublisher.stopRecord();
            //关闭
        } catch (Exception e1) {
            //
        }
    }

    @Override
    public void onRecordPause() {
    }

    @Override
    public void onRecordResume() {
    }

    @Override
    public void onRecordStarted(String msg) {
        ToastApp.showToast(msg);
    }

    @Override
    public void onRecordFinished(String msg) {
        ToastApp.showToast(msg);
    }

    @Override
    public void onRecordIllegalArgumentException(IllegalArgumentException e) {
        handleException(e);
    }

    @Override
    public void onRecordIOException(IOException e) {
        handleException(e);
    }

    @Override
    public void onRtmpConnecting(String msg) {
        ToastApp.showToast(msg);
    }

    @Override
    public void onRtmpConnected(String msg) {
        ToastApp.showToast(msg);
    }

    @Override
    public void onRtmpVideoStreaming() {

    }

    @Override
    public void onRtmpAudioStreaming() {

    }

    @Override
    public void onRtmpStopped() {
    }

    @Override
    public void onRtmpDisconnected() {
        mPublisher.startCamera();
    }

    @Override
    public void onRtmpVideoFpsChanged(double fps) {

    }

    @Override
    public void onRtmpVideoBitrateChanged(double bitrate) {

    }

    @Override
    public void onRtmpAudioBitrateChanged(double bitrate) {

    }

    @Override
    public void onRtmpSocketException(SocketException e) {
        handleException(e);
    }

    @Override
    public void onRtmpIOException(IOException e) {
        handleException(e);
    }

    @Override
    public void onRtmpIllegalArgumentException(IllegalArgumentException e) {
        handleException(e);
    }

    @Override
    public void onRtmpIllegalStateException(IllegalStateException e) {
        handleException(e);
    }
}
